package com.rado.tf.gui.view.menu.impl;

import com.rado.tf.gui.event.UIEvent;
import com.rado.tf.gui.event.UIEventTypes;
import com.rado.tf.gui.view.application.IApplicationPresenter;
import com.rado.tf.gui.view.common.View;
import com.rado.tf.gui.view.dashboard.impl.DashboardViewImpl;
import com.rado.tf.gui.view.menu.IMenuPresenter;
import com.rado.tf.gui.view.menu.IMenuView;
import com.rado.tf.gui.view.newGroups.impl.NewGroupsViewImpl;
import com.rado.tf.gui.view.sampleValidation.impl.SampleValidationViewImpl;
import com.rado.tf.gui.view.userList.impl.UserListViewImpl;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.ui.ComponentContainer;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * Created by parado on 17.03.14.
 */
@Component(MenuPresenterImpl.BEAN_NAME)
public class MenuPresenterImpl implements IMenuPresenter
{
   public static final String BEAN_NAME = "menuPresenter";

   @Resource
   private IMenuView menuView;

   @Resource
   private ApplicationEventPublisher applicationEventPublisher;

   @Override
   public View getViewDisplay()
   {
      return menuView;
   }

   @Override
   @PostConstruct
   public void bind()
   {
      menuView.addMenuItemClickListener(new ItemClickEvent.ItemClickListener()
      {
         @Override
         public void itemClick(ItemClickEvent event)
         {
            if (DashboardViewImpl.BEAN_NAME.equals(event.getItemId()))
            {
               applicationEventPublisher
                       .publishEvent(new UIEvent(MenuPresenterImpl.this, UIEventTypes.MENU_DASHBOARD_SELECTED));
            }
            else if (NewGroupsViewImpl.BEAN_NAME.equals(event.getItemId()))
            {
               applicationEventPublisher
                       .publishEvent(new UIEvent(MenuPresenterImpl.this, UIEventTypes.MENU_NEW_GROUPS_SELECTED));
            }
            else if (UserListViewImpl.BEAN_NAME.equals(event.getItemId()))
            {
               applicationEventPublisher
                       .publishEvent(new UIEvent(MenuPresenterImpl.this, UIEventTypes.MENU_USER_LIST_SELECTED));
            }
            else if (SampleValidationViewImpl.BEAN_NAME.equals(event.getItemId()))
            {
               applicationEventPublisher
                       .publishEvent(new UIEvent(MenuPresenterImpl.this, UIEventTypes.MENU_SAMPLE_VALIDATION_SELECTED));
            }
         }
      });
   }

   @Override
   public void onApplicationEvent(ApplicationEvent event)
   {
      if (event instanceof UIEvent)
      {
         if (UIEventTypes.APPLICATION_VIEW_ATTACHED == ((UIEvent) event).getEventType())
         {
            handleApplicationViewAttached((UIEvent) event);
         }
      }
   }

   private void handleApplicationViewAttached(UIEvent event)
   {
      ComponentContainer menu = ((IApplicationPresenter) event.getSource()).getViewDisplay().getMenuContainer();
      menu.removeAllComponents();
      menu.addComponent(menuView.getRoot());
      menuView.selectMenuItem(DashboardViewImpl.BEAN_NAME);
      applicationEventPublisher.publishEvent(new UIEvent(MenuPresenterImpl.this, UIEventTypes.MENU_DASHBOARD_SELECTED));
   }
}
